// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,

    properties: {
        maxMoveSpeed: 0,//最大移动速度
        accel: 0,//加速度

        bullesprefab://子弹预制体
        {
            default: null,
            type: cc.Prefab,
        },

        qwqw:
        {
            default: null,
            type: cc.Canvas,
        },

        scorelaber: {
            default: null,
            type: cc.Label,
        },

        scoreAudio: {
            default: null,
            type: cc.AudioClip,
        },

        // bgmAudio: {
        //     default: null,
        //     type: cc.AudioClip,
        // },
    },


    keyDown(event) {
        switch (event.keyCode) {
            case cc.macro.KEY.a:
                this.accleft = true;
                break;
            case cc.macro.KEY.d:
                this.accright = true;
                break;
            case cc.macro.KEY.w:
                this.accup = true;
                break;
            case cc.macro.KEY.s:
                this.accdown = true;
                break;
            case cc.macro.KEY.j:
                this.accj = true;
                // var bulletNode = cc.instantiate(this.bullesprefab);//生成子弹//预制体实例化
                // this.node.addChild(bulletNode);//子弹添加到父节点
                // bulletNode.setPosition(0, 100);//子弹位置
                // var rgb = bulletNode.getComponent(cc.RigidBody);//声明子弹的刚体
                // rgb.applyLinearImpulse(cc.v2(0,35), rgb.getWorldCenter());//给子弹设置一个向上的冲量
                break;
        }
    },


    keyUp(event) {
        switch (event.keyCode) {
            case cc.macro.KEY.a:
                this.accleft = false;
                this.speed = 0;
                break;
            case cc.macro.KEY.d:
                this.accright = false;
                this.speed = 0;
                break;
            case cc.macro.KEY.w:
                this.accup = false;
                this.hid = 0;
                break;
            case cc.macro.KEY.s:
                this.accdown = false;
                this.hid = 0;
                break;
            case cc.macro.KEY.j:
                this.accj = false;
                break;
        };
    },
    // LIFE-CYCLE CALLBACKS:

    onLoad() {
        this.speed = 0;
        this.hid = 0;
        this.accleft = false;//判断是否按下A键
        this.accright = false;//判断是否按下D键
        this.accup = false;//判断是否按下W键
        this.accdown = false;//判断是否按下S键
        this.accj = false;//判断是否按下J键
        cc.director.getPhysicsManager().enabled = true;//开启物理系统

        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, this.keyDown, this);//事件侦听
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_UP, this.keyUp, this);//事件侦听

        //cc.audioEngine.playEffect(this.bgmAudio, false);
    },

    start() {
        this.hp = 100;
        //this.score = 0;
        this.scorelaber.string = 'Score:' + G.score.toString();
    },

    onBeginContact: function (contact, selfCollider, otherCollider) {
        if (otherCollider.node.name == 'prop') {
            this.gainScore();
            cc.audioEngine.playEffect(this.scoreAudio, false);
            otherCollider.node.destroy();
        }else if(otherCollider.node.name == 'bullet'){
            return;
        }
         else {
            selfCollider.node.destroy();
            otherCollider.node.destroy();
            cc.director.loadScene('UI');
            G.score = 0;
        }
    },

    gainScore() {
        G.score += 10;
        this.scorelaber.string = 'Score:' + G.score.toString();
    },

    update(dt) {
        if (this.accleft) {
            this.speed -= this.accel * dt;
        }
        else if (this.accright) {
            this.speed += this.accel * dt;
        }

        if (this.accup) {
            this.hid += this.accel * dt;
        }
        else if (this.accdown) {
            this.hid -= this.accel * dt;
        }

        if (this.accj) {
            var bulletNode = cc.instantiate(this.bullesprefab);//生成子弹//实例化子弹
            this.node.addChild(bulletNode);//子弹添加到父节点
            bulletNode.setPosition(0, 80);//子弹位置
            var rgb = bulletNode.getComponent(cc.RigidBody);//声明子弹的刚体
            rgb.applyLinearImpulse(cc.v2(0, 50), rgb.getWorldCenter())
        }

        if (Math.abs(this.speed) > this.maxMoveSpeed) {
            this.speed = Math.sign(this.speed) * this.maxMoveSpeed;
        }
        if (Math.abs(this.hid) > this.maxMoveSpeed) {
            this.hid = Math.sign(this.hid) * this.maxMoveSpeed;
        }

        var d = this.speed * dt;
        this.node.x += d;

        if (this.node.x > 210) {
            this.node.x = 210;
        }
        if (this.node.x < -210) {
            this.node.x = -210;
        }

        var g = this.hid * dt;
        this.node.y += g;

        if (this.node.y > 270) {
            this.node.y = 270;
        }
        if (this.node.y < -280) {
            this.node.y = -280;
        }

        //var billet = cc.instantiate(this.bullesprefab);

    },
});
